import { cloneDeep, lodashMap as map, template as lodashTemplate } from "lodash-es";
import { failres, okres } from "../../config/resUtil.js";

import { Controller, App, Post, Body } from "@midwayjs/core";
import { Application } from "../../interface.js";

// 一些默认的公共的操作
export class live_anchor_live_listController {
  @App("koa")
  app: Application;



  async querybuildcomon(query, body, ispage = true) {
    
        if (body?.['id']) {
        query.andWhere("id", body?.['id']);
      }
      
        if (body?.['anchor_id']) {
        query.andWhere("anchor_id", body?.['anchor_id']);
      }
      
        if (body?.['anchor_code']) {
        query.andWhere("anchor_code", body?.['anchor_code']);
      }
      
        if (body?.['shop_code']) {
        query.andWhere("shop_code", body?.['shop_code']);
      }
      
        if (body?.['platform']) {
        query.andWhere("platform", body?.['platform']);
      }
      
        if (body?.['star_time']) {
        query.andWhere("star_time", body?.['star_time']);
      }
      
        if (body?.['end_time']) {
        query.andWhere("end_time", body?.['end_time']);
      }
      
        if (body?.['fans_num']) {
        query.andWhere("fans_num", body?.['fans_num']);
      }
      
        if (body?.['gmv']) {
        query.andWhere("gmv", body?.['gmv']);
      }
      
        if (body?.['gpm']) {
        query.andWhere("gpm", body?.['gpm']);
      }
      
        if (body?.['refund_money']) {
        query.andWhere("refund_money", body?.['refund_money']);
      }
      
        if (body?.['watch_num']) {
        query.andWhere("watch_num", body?.['watch_num']);
      }
      
        if (body?.['transaction_rate']) {
        query.andWhere("transaction_rate", body?.['transaction_rate']);
      }
      
        if (body?.['user_value']) {
        query.andWhere("user_value", body?.['user_value']);
      }
      
        if (body?.['exposure_num']) {
        query.andWhere("exposure_num", body?.['exposure_num']);
      }
      
        if (body?.['free_watch_num']) {
        query.andWhere("free_watch_num", body?.['free_watch_num']);
      }
      
        if (body?.['pay_watch_num']) {
        query.andWhere("pay_watch_num", body?.['pay_watch_num']);
      }
      
        if (body?.['strike_num']) {
        query.andWhere("strike_num", body?.['strike_num']);
      }
      
        if (body?.['avg_online']) {
        query.andWhere("avg_online", body?.['avg_online']);
      }
      
        if (body?.['max_online']) {
        query.andWhere("max_online", body?.['max_online']);
      }
      
        if (body?.['live_exposure_num']) {
        query.andWhere("live_exposure_num", body?.['live_exposure_num']);
      }
      
        if (body?.['deal_num']) {
        query.andWhere("deal_num", body?.['deal_num']);
      }
      
        if (body?.['goods_exposure_num']) {
        query.andWhere("goods_exposure_num", body?.['goods_exposure_num']);
      }
      
        if (body?.['goods_click_num']) {
        query.andWhere("goods_click_num", body?.['goods_click_num']);
      }
      
        if (body?.['interact_num']) {
        query.andWhere("interact_num", body?.['interact_num']);
      }
      
        if (body?.['comment_num']) {
        query.andWhere("comment_num", body?.['comment_num']);
      }
      
        if (body?.['likes_num']) {
        query.andWhere("likes_num", body?.['likes_num']);
      }
      
        if (body?.['share_num']) {
        query.andWhere("share_num", body?.['share_num']);
      }
      
        if (body?.['new_fans']) {
        query.andWhere("new_fans", body?.['new_fans']);
      }
      
        if (body?.['fans_pay_ratio']) {
        query.andWhere("fans_pay_ratio", body?.['fans_pay_ratio']);
      }
      
        if (body?.['purchase_fans']) {
        query.andWhere("purchase_fans", body?.['purchase_fans']);
      }
      
        if (body?.['purchase_nonfans']) {
        query.andWhere("purchase_nonfans", body?.['purchase_nonfans']);
      }
      
        if (body?.['nonpurchase_fans']) {
        query.andWhere("nonpurchase_fans", body?.['nonpurchase_fans']);
      }
      
        if (body?.['nonpurchase_nonfans']) {
        query.andWhere("nonpurchase_nonfans", body?.['nonpurchase_nonfans']);
      }
      
        if (body?.['interact_ratio']) {
        query.andWhere("interact_ratio", body?.['interact_ratio']);
      }
      
        if (body?.['attention_ratio']) {
        query.andWhere("attention_ratio", body?.['attention_ratio']);
      }
      
        if (body?.['live_folw_ratio']) {
        query.andWhere("live_folw_ratio", body?.['live_folw_ratio']);
      }
      
        if (body?.['attention_flow_ratio']) {
        query.andWhere("attention_flow_ratio", body?.['attention_flow_ratio']);
      }
      
        if (body?.['video_flow_ratio']) {
        query.andWhere("video_flow_ratio", body?.['video_flow_ratio']);
      }
      
        if (body?.['shop_flow_ratio']) {
        query.andWhere("shop_flow_ratio", body?.['shop_flow_ratio']);
      }
      
        if (body?.['other_flow_ratio']) {
        query.andWhere("other_flow_ratio", body?.['other_flow_ratio']);
      }
      
        if (body?.['shopwindow_flow_ratio']) {
        query.andWhere("shopwindow_flow_ratio", body?.['shopwindow_flow_ratio']);
      }
      
        if (body?.['freeflow_flow_ratio']) {
        query.andWhere("freeflow_flow_ratio", body?.['freeflow_flow_ratio']);
      }
      
        if (body?.['riverflow_pc_flow_ratio']) {
        query.andWhere("riverflow_pc_flow_ratio", body?.['riverflow_pc_flow_ratio']);
      }
      
        if (body?.['new_buy_bunch']) {
        query.andWhere("new_buy_bunch", body?.['new_buy_bunch']);
      }
      
        if (body?.['product_click_pay_ucnt_ratio']) {
        query.andWhere("product_click_pay_ucnt_ratio", body?.['product_click_pay_ucnt_ratio']);
      }
      
        if (body?.['avg_watch_duration']) {
        query.andWhere("avg_watch_duration", body?.['avg_watch_duration']);
      }
      
        if (body?.['watch_cnt_show_ratio']) {
        query.andWhere("watch_cnt_show_ratio", body?.['watch_cnt_show_ratio']);
      }
      
        if (body?.['pay_cnt']) {
        query.andWhere("pay_cnt", body?.['pay_cnt']);
      }
      
        if (body?.['buyin_gmv']) {
        query.andWhere("buyin_gmv", body?.['buyin_gmv']);
      }
      
        if (body?.['remarks']) {
        query.andWhere("remarks", body?.['remarks']);
      }
      
        if (body?.['tenant_type']) {
        query.andWhere("tenant_type", body?.['tenant_type']);
      }
      
        if (body?.['tenant_id']) {
        query.andWhere("tenant_id", body?.['tenant_id']);
      }
      
        if (body?.['del_state']) {
        query.andWhere("del_state", body?.['del_state']);
      }
      
        if (body?.['add_user']) {
        query.andWhere("add_user", body?.['add_user']);
      }
      
        if (body?.['add_user_id']) {
        query.andWhere("add_user_id", body?.['add_user_id']);
      }
      
        if (body?.['add_time']) {
        query.andWhere("add_time", body?.['add_time']);
      }
      
        if (body?.['edit_user']) {
        query.andWhere("edit_user", body?.['edit_user']);
      }
      
        if (body?.['edit_user_id']) {
        query.andWhere("edit_user_id", body?.['edit_user_id']);
      }
      
        if (body?.['last_edit_time']) {
        query.andWhere("last_edit_time", body?.['last_edit_time']);
      }
      
        if (body?.['server_number']) {
        query.andWhere("server_number", body?.['server_number']);
      }
      


      if (ispage) {
            if (body?.pageSize) {
              query.limit(body?.pageSize)
            }


var pageNum=body?.pageNum ||1
            if (pageNum) {
              query.offset( (pageNum-1 )* body?.pageSize )
            }
      }



  }

  getDb() {
    return this.app['erpdb']
  }



  async getlist(body) {
    console.log("开始 列表 ");

    var query = this.getDb().select().from("live_anchor_live_list");
    var query1 = this.getDb().select().from("live_anchor_live_list");

    //获取默认字段
    this.querybuildcomon(query, body);
    this.querybuildcomon(query1, body, false);
    var totalresults = await query1.count('id')
    var total = totalresults?.[0]?.['count(`id`)'];
    console.log('total :', total);

    query.orderBy('id','desc')

    return okres(await query, { total });
  }

  async getbyitem(body) {
    console.log("开始 获取单个 id:", body?.id);
    var query = this.getDb().select("*").from("live_anchor_live_list");
    console.log("body :", body);


    if (!body?.id) {
      return okres(null);
    }

    if ((!body) || !Object.keys(body).length) {
      return okres(null);
    }

    if (body?.id) {
      query.andWhere("id", '=', body?.id);
    } else {

    }


    console.log("query :", query);

    var lists = await query;
    var one = lists?.[0];
    console.log('开始sq沙溪村  :', one);

    return okres(one);
  }

  async 删除(body) {
    if (!body.id) {
      return failres('没有id')
    }
    var delresult = await this.getDb()('live_anchor_live_list').where('id', body.id).del();
    return okres(delresult);
  }

  async addorupdate(body) {
    console.log("开始 新增or修改 ");
    var rsiltonme = await this.getbyitem(body);
    var one = rsiltonme && rsiltonme.data;
    console.log("one :", one);

    var result = undefined;
    if (one?.id) {
      var __params = cloneDeep(body)
      delete __params.id
      result = await this.getDb()("live_anchor_live_list").where("id", '=', one.id).update(__params);
    } else {
      // 新增
      result = await this.getDb()("live_anchor_live_list").insert(body);
    }
    return okres(result);
  }

}